package com.boe.csb.web.model;


import com.boe.csb.core.common.exception.CSBErrorCode;

import java.util.Map;

/**
 * ResultModel 返回结果类
 *
 * Created by tianjin.lp on 16/8/31.
 */
public class ResultModel<T> {

    private int resultCode = 200;

    private T data;

    private Map<String, String> fieldErrMsg;

    private String resultMsg = CSBErrorCode.SUCCESS.getMessage();

    private String returnErrorStackTrace;

    public static <T> ResultModel<T> success(T data) {
        return success(data, "");
    }

    public static <T> ResultModel<T> success(T data, String msg) {
        ResultModel<T> res = new ResultModel<T>();
        res.setResultCode(CSBErrorCode.SUCCESS.getCode());
        res.setData(data);
        res.setResultMsg(msg);
        return res;
    }

    public static <T> ResultModel<T> fail(CSBErrorCode errors, String msg) {
        ResultModel<T> res = new ResultModel<T>();
        res.setResultCode(errors.getCode());
        res.setData(null);
        
        String resultMsg = msg == null ? "" : (" " + msg);
        resultMsg = errors.getMessage() + resultMsg;
        res.setResultMsg(resultMsg);
        return res;
    }

    public int getResultCode() {
        return resultCode;
    }

    public void setResultCode(int resultCode) {
        this.resultCode = resultCode;
    }

    public String getReturnErrorStackTrace() {
        return returnErrorStackTrace;
    }

    public void setReturnErrorStackTrace(String returnErrorStackTrace) {
        this.returnErrorStackTrace = returnErrorStackTrace;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }

    public boolean isSuccess() {
        return resultCode == 0;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Map<String, String> getFieldErrMsg() {
        return fieldErrMsg;
    }

    public void setFieldErrMsg(Map<String, String> fieldErrMsg) {
        this.fieldErrMsg = fieldErrMsg;
    }


    @Override
    public String toString() {
        return "ResultModel{" +
                "resultCode=" + resultCode +
                ", data=" + data +
                ", fieldErrMsg=" + fieldErrMsg +
                ", resultMsg='" + resultMsg + '\'' +
                ", returnErrorStackTrace='" + returnErrorStackTrace + '\'' +
                '}';
    }
}
